package com.shanghaionstar.servicepackage;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.EncryptUtil;
import com.onstar.cn.framework.annotation.ContentView;
import com.onstar.cn.framework.annotation.FindViewById;
import com.onstar.cn.framework.annotation.OnClick;
import com.onstar.cn.framework.mvp.base.core.BaseActivity;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.mag.enumtype.PackageType;
import com.onstar.cn.map.util.ToastUtil;
import com.onstar.cn.payme.response.GetPackageListResponse;
import com.onstar.cn.weight.ProgressLayout;
import com.shanghaionstar.R;
import com.shanghaionstar.activity.webview.handler.CommonWebViewActivity;
import com.shanghaionstar.activity.webview.handler.WebDataEntity;
import com.shanghaionstar.carmanage.OrderRecordActivity;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.servicepackage.adapter.OnstarPkgListAdapter;
import com.shanghaionstar.servicepackage.presenter.OnstarPkgPresenter;
import com.shanghaionstar.servicepackage.view.OnstarPkgView;
import com.shanghaionstar.swipe.SwipeActivity;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.weight.CommonNoDatasLayout;
import com.shanghaionstar.weight.TopLayout;

/**
 * Created by Yonglang Hong on 2016/8/8.
 */
@ContentView(R.layout.package_activity_onstar_list)
public class OnStarPackageListActivity extends SwipeActivity implements OnstarPkgView,View.OnClickListener{

    @FindViewById
    private TopLayout top_layout;
    @FindViewById
    ProgressLayout pl_loading;
    @FindViewById
    View rl_packageInfo;
    @FindViewById
    CommonNoDatasLayout cndl_noDatas;
    @FindViewById
    TextView spare_day_one;
    @FindViewById
    ListView list_service;

    OnstarPkgListAdapter adapter;

    private OnstarPkgPresenter packagePresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
    }

    @Override
    public void finish() {
        super.finish();
        BusinessUtil.sendReportByClickType(this, FunctionNameFindId.OnstarPackage_Back.getStringValue());
    }

    private void init(){
        setAdapter();
        loadPackages();
        setUpTopLayout();
    }

    private void setUpTopLayout(){
        top_layout.setOnRightTextClickLstener(getString(R.string.option_order_record), new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BusinessUtil.sendReportByClickType(OnStarPackageListActivity.this, FunctionNameFindId.OnstarPackage_OrderHistory.getStringValue());
                Intent paymeIntent = new Intent(OnStarPackageListActivity.this, OrderRecordActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("packageType", PackageType.CORE);
                paymeIntent.putExtras(bundle);
                startActivity(paymeIntent);
            }
        });
    }

    private void setAdapter(){
        if (adapter == null)
            adapter = new OnstarPkgListAdapter(this);
        list_service.setAdapter(adapter);
    }

    @OnClick({R.id.img_ebIcon,R.id.btn_buyMore})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.img_ebIcon:
                gotoSpecialOfferPage();
                break;
            case R.id.btn_buyMore:
                gotoBuyMorePage();
                break;
        }
    }

    /**
     * 跳转优惠活动页
     */
    private void gotoSpecialOfferPage(){
        BusinessUtil.sendReportByClickType(OnStarPackageListActivity.this, FunctionNameFindId.OnstarPackage_MorningBird.getStringValue());
        String url = Constants.CURRENT_PREFIX_H5_URL + Constants.EB_RULE_URL;
//        Intent intent2 = new Intent(this, OnstarWebViewActivity.class);
//        HashMap<String, String> webViewParam = new HashMap<>();
//        webViewParam.put(OnstarWebViewActivity.URL, url);
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_PROXY_NAME, FavorableWebViewProxy.class);
//        intent2.putExtra(OnstarWebViewActivity.WEBVIEWS_MAP,webViewParam);
//        startActivity(intent2);
        WebDataEntity entity = new WebDataEntity();
        entity.setLinkUrl(url);
        entity.setTopTitle(getString(R.string.eb_title));
        CommonWebViewActivity.launchCommonWeb(this,entity);
    }

    /**
     * 购买更多
     */
    private void gotoBuyMorePage(){
        BusinessUtil.sendReportByClickType(this, FunctionNameFindId.OnstarPackage_BuyServicePackage.getStringValue());
        Intent i = new Intent(this, OnstarBuyPackageActivity.class);
        Bundle b = new Bundle();
        b.putSerializable("packageType", PackageType.CORE);
        i.putExtras(b);
        startActivity(i);
    }

    private void loadPackages(){
        pl_loading.setVisibility(View.VISIBLE);
        getPackagePresenter().getOnstarPackages();
    }

    public OnstarPkgPresenter getPackagePresenter() {
        if (packagePresenter == null){
            packagePresenter = new OnstarPkgPresenter(getScheduler(),this);
            packagePresenter.setPackageView(this);
        }
        return packagePresenter;
    }

    @Override
    public void showPackageList(GetPackageListResponse listResponse) {
        if (listResponse.getPackageInfoList() == null
                || listResponse.getPackageInfoList().isEmpty()){
            showEmptyView(View.VISIBLE,R.string.no_core_tips,R.drawable.no_core_datas);
            list_service.setVisibility(View.GONE);
        }else {
            cndl_noDatas.setVisibility(View.GONE);
            list_service.setVisibility(View.VISIBLE);
            adapter.addAll(listResponse.getPackageInfoList());
        }
        spare_day_one.setText(listResponse.getTotalRemainingDay());
        pl_loading.setVisibility(View.GONE);
        rl_packageInfo.setVisibility(View.VISIBLE);
    }

    @Override
    public void showNoDatas() {
        pl_loading.setVisibility(View.GONE);
        list_service.setVisibility(View.GONE);
        rl_packageInfo.setVisibility(View.GONE);
        showEmptyView(View.VISIBLE,R.string.no_core_tips,R.drawable.no_core_datas);
        cndl_noDatas.setOnRefreshListener(null);
    }

    @Override
    public void showError(String err) {
        pl_loading.setVisibility(View.GONE);
        list_service.setVisibility(View.GONE);
        rl_packageInfo.setVisibility(View.GONE);
        showEmptyView(View.VISIBLE,R.string.no_set_meal_data,R.drawable.common_refresh_data);
        cndl_noDatas.setOnRefreshListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadPackages();
            }
        });
        if (!TextUtils.isEmpty(err))
            ToastUtil.show(this,err);
    }

    private void showEmptyView(int visible,int tipId,int imgId){
        cndl_noDatas.setVisibility(visible);
        cndl_noDatas.setHintText(tipId);
        cndl_noDatas.setRefreshImage(imgId);
        cndl_noDatas.showIcon(true);
    }

    @Override
    public BasePresenter getPresenter() {
        return null;
    }
}
